<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
use app\index\model\UserModel;

class Index extends Controller
{
	 //登陆
    public function index()
    {
//  	$list = db('info_agent')->where('status',1)->select();
//  	$this->assign('list',$list);
//  	$this->assign('empty','<span class="empty">没有数据</span>');
    	return  $this->fetch();
    }
	//首页商家
	public function get_agents(){
		$longitude = input('longitude');
		$latitude = input('latitude');
		$order = ' order by juli asc ';
		$where =" where status = 1 and oem_id = ".config('cut_oem_id');
		$field='a.start_time,a.end_time,a.id,a.agent_name,a.add_time,a.logo,a.address,a.longitude,a.latitude,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(('.$latitude.'*PI()/180-latitude*PI()/180)/2),2)+COS('.$latitude.'*PI()/180)*COS(latitude*PI()/180)*POW(SIN(('.$longitude.'*PI()/180-longitude*PI()/180)/2),2)))*1000) AS juli';
		$sql = 'select '.$field.' from info_agent as a '.$where.$order;
		$resultList=Db::query($sql);
		return json(["list"=>$resultList]);
	}
    //登陆
    public function login()
    {
    	if(request()->isPost()){
    		
    		$param=input('post.');
    		
    		$where['mobile'] = $param['phone'];
    		$where['password'] = md5_salt($param['pwd']);
    		$where['oem_id'] = config('cut_oem_id');
    		
    		$user_info = db('info_user')->where($where)->find();
    		if($user_info){
    			if($user_info['status'] == 1){
    				session('info_user', $user_info);
    				 $this->success('登录成功','index/member/index');
    			}else{
    				$this->error('账号异常，已冻结!');
    			}
    		}else{
    			$this->error('用户名密码错误');
    		}
    	}
    	
       return  $this->fetch();
    }
    //注册
    public function regist()
    {
    	//业务员信息
		$oem_info_salesman = db('info_salesman')->where('id',input('salesman_id'))->find();
		$this->assign('oem_info_salesman',$oem_info_salesman);
    	
    	if(request()->isPost()){
    		$result = check_sendcode($_POST['mobile'], $_POST['yzm'], '发送成功');

			if($result != 1) {
				$this->error('验证码错误或过期');
			}
			
			if(input('salesman_id') && input('salesman_id')>0){
				$user_info = db('info_user')
    						->where('mobile',$_POST['mobile'])
    						->where('oem_id',$oem_info_salesman['oem_id'])
    						->find();
			}else{
				$user_info = db('info_user')
    						->where('mobile',$_POST['mobile'])
    						->where('oem_id',config('cut_oem_id'))
    						->find();
			}
	    	if($user_info){
	    		$this->error('该手机号码已经注册');
	   		}
			$pwd = md5_salt($_POST['pwd']);
			//默认业务员
			if($_POST['salesman_id']>0){
				$_POST['salesman_id'] = 1;
			}
			
			if($oem_info_salesman){
				$_POST['agency_id1'] 	= $oem_info_salesman['agency_id1'];
				$_POST['agency_id2'] 	= $oem_info_salesman['agency_id2'];
				$_POST['salesman_id'] 	= $oem_info_salesman['id'];
			}
			
			$_POST['password'] 		= $pwd;
			$_POST['level'] 		= $_POST['fromuid'];
			$_POST['oem_id'] 		= config('cut_oem_id');
			$_POST['add_time']		= now_time();
			
			$user_model = new UserModel();
			
			$rest=$user_model->allowField(true)->save($_POST);
			
			if($rest){
				$user_id = db('info_user')->getLastInsID();
				
				$user_info = db('info_user')->where('id',$user_id)->find();
				
				session('info_user',$user_info);
				
				$this->success('登录成功','index/member/index');
			}
			$this->error('注册失败');
    	}else{
    		$type=get_brower_type();//判断是微信还是支付宝打开
	    	$redirect='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
	    	if($type==1){
				$auth_code=input("auth_code");
				$url ='https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=2017092808972929&scope=auth_base&redirect_uri='.urlencode($redirect);			
			}else{
				$auth_code=input("code");
				$url ='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.config('wxpay_appid').'&redirect_uri='.urlencode($redirect).'&response_type=code&scope=snsapi_base&state=STATE&connect_redirect=1#wechat_redirect';
			}
			if(!$auth_code){
				Header("Location: $url");
				exit;
			}else{
				if($type==1){//支付宝openid
					$pay_type = 3;
					$field="alipay_id";
					$openid=user_ali_id($auth_code);
					
				}elseif($type==2){//微信openid
					$pay_type = 2;
					$field="openid";
					$openid=user_wx_id($auth_code);
				}
				
				$openid_info['key_type'] = $pay_type;
				$openid_info['key_id'] = $openid;
				$this->assign('openid_info',$openid_info);
			}
    	}
       return  $this->fetch();
    }
    //修改密码
    public function return_pwd()
    {
    	if(request()->isPost()){
    		
    		$result = check_sendcode($_POST['mobile'], $_POST['yzm'], '发送成功');

			if($result != 1) {
				$this->error('验证码错误或过期');
			}
    		$user_info = db('info_user')
    					->where('mobile',$_POST['mobile'])
    					->where('oem_id',$_POST['oem_id'])
    					->find();
	    	if($user_info){
	    			$pwd = md5_salt($_POST['pwd']);
	    			$result = Db::table('info_user')->where('id', $user_info['id'])->update(['password' => $pwd]);
	    			if($result){
	    				$this->success('修改成功','index/member/login');
	    			}else{
	    				$this->error('修改失败');
	    			}
	   		}else{
	   			$this->error('用户不存在');
	   		}
    	}
    	
       return  $this->fetch();
    }
    
    /**
     * 获取验证码
     * 注意防盗刷
     * 
     */
    function verify_code(){
    	
    	if(request()->isPost()){
    		return get_code($_POST['mobile'],1);
    	}
    }
    
    
    //绑定手机号
    function my_mobile(){
    	//获取当前域名
    	//$domain = $_SERVER['HTTP_HOST'];
    	//查找当前域名的oem
    	$oem_info = db('info_oem')->where('id',config('cut_oem_id'))->find();
    	//$oem_info = db('info_oem')->where('web_url','like',$domain)->find();
    	$this->assign('oem_info',$oem_info);
    	
    	if(request()->isPost()){
    		$result = check_sendcode($_POST['mobile'], $_POST['verifycode'], '发送成功');

			if($result != 1) {
				$this->error('验证码错误或过期');
			}
    		$user_info = db('info_user')
    						->where('mobile',$_POST['mobile'])
    						->where('oem_id',$oem_info['id'])
    						->find();
	    	if($user_info){
	    		$this->error('该手机号码已经注册');
	   		}
	   		$password = md5_salt($_POST['password']);
	   		$info_user = session('info_user');
    		$reult = Db::table('info_user')->where('id', $info_user['id'])->update(['mobile' => $_POST['mobile'],'password'=>$password]);
    		if($reult){
    			$this->success('绑定成功');
    		}else{
    			$this->error('绑定失败');
    		}
    	}
    	return  $this->fetch();
    }
    
    /**
     * 退出登录
     */
    public function logout() {
        session(null);
        $this->success('退出成功', 'index/member/login');
    } 
    //shop_info
    public function shop_info()
    {
    	$data = db('info_agent')->where('id',input('agent_id'))->find();
		$ads=db('agent_advertisement')->where('agent_id',input('agent_id'))->select();
    	$this->assign('data',$data);
		$this->assign('ads',$ads);
        return  $this->fetch();
    }
}
